package com.test.study;

import java.util.ArrayList;
import java.util.List;

/**
 * created by lpw
 * on 2020/5/29
 */
public class LiPengWei {
    public static void main(String[] args) {
        josephusProblem(41,3);
    }

    /**
     *
     * @param number 人数
     * @param n 规则，即第几个人死
     */
    private static void josephusProblem(int number, int n) {
        List peoples =new ArrayList();
        //给每个人编号
        for (int i = 1; i <= number; i++) {
            peoples.add(i);
        }
        int count=1;
        while (peoples.size()>1){
            //一个中间数组，用于存放每一圈杀死的人
            List temp=new ArrayList();
            for (int i = 0; i < peoples.size(); i++) {
                if (count==n){
                    temp.add(peoples.get(i));
                    count=1;
                    continue;
                }
                count++;
            }
            //循环完毕后杀掉
            peoples.removeAll(temp);
        }

        for (int i = 0; i < peoples.size(); i++) {
            System.out.println("存活的人："+peoples.get(i));
        }

    }
}
